package com.loongtech.bi.action.system;

import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.loongtech.bi.entity.system.EntityGameParam;
import com.loongtech.bi.entity.system.EntityGameParamType;
import com.loongtech.bi.manager.system.GameParamConfigManager;
import com.loongtech.bi.manager.system.GameParamTypeConfigManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.support.Session;

/**
 * 游戏参数类型配置
 * 
 * @author marin
 * @date 2015-09-29
 */
@Controller
@Scope("view")
public class GameParamTypeConfigAction {
	@Autowired
	private GameParamTypeConfigManager gameParamTypeManager;
	
	private EntityGameParamType entity;
	private List<EntityGameParamType> list;
	
	@Autowired
	private Session session;
	@Autowired
	private SysLogManager sysLogManager;
	
	@Autowired
	public void init() {
		sysLogManager.addLog(session.getUserName(), "游戏参数类型配置");
		
		initData();
	}
	
	private void initData() {
		entity = new EntityGameParamType();
		list = GameParamTypeConfigManager.GAME_PARAM_TYPE_LIST;
	}

	public void add() {
		FacesContext context = FacesContext.getCurrentInstance();
		
		if (entity.getName() == null || "".equals(entity.getName())) {
			context.addMessage(null, new FacesMessage("值不能为空！"));
			return;
		}
		
		if (GameParamTypeConfigManager.canAdd(entity)) {
			gameParamTypeManager.persist(entity);
			context.addMessage(null, new FacesMessage("添加成功！"));
			
			gameParamTypeManager.init();
			initData();
		} else {
			context.addMessage(null, new FacesMessage("名称重复！"));
		}
	}

	public void modify(EntityGameParamType e) {
		FacesContext context = FacesContext.getCurrentInstance();
		if (GameParamTypeConfigManager.canAdd(e)) {
			gameParamTypeManager.merge(e);
			context.addMessage(null, new FacesMessage("修改成功！"));
		} else {
			context.addMessage(null, new FacesMessage("名称重复或未修改！"));
		}
		
		gameParamTypeManager.init();
		initData();
	}
	
	public void delete(int id) {
		FacesContext context = FacesContext.getCurrentInstance();
		
		Map<String, EntityGameParam> tmp = GameParamConfigManager.TYPE2ID2GAMEPARAM_MAP.get(id);
		if (tmp != null && tmp.size() > 0) {
			context.addMessage(null, new FacesMessage("已关联数据，无法删除！"));
			return;
		}
		
		gameParamTypeManager.removeById(id);
		gameParamTypeManager.init();
		initData();
		
		context.addMessage(null, new FacesMessage("删除成功！"));
	}

	public EntityGameParamType getEntity() {
		return entity;
	}

	public void setEntity(EntityGameParamType entity) {
		this.entity = entity;
	}

	public List<EntityGameParamType> getList() {
		return list;
	}

	public void setList(List<EntityGameParamType> list) {
		this.list = list;
	}
}
